package com.yantu.controller.wx;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yantu.bean.User;
import com.yantu.bean.UserEventLog;
import com.yantu.mapper.UserEventLogMapper;
import com.yantu.mapper.UserMapper;
import com.yantu.util.JWTUtil;
import com.yantu.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-14
 */
@RestController
@Api("用户日志")
@RequestMapping("/wx")
public class WxUserFatherController {
    @Autowired
    JWTUtil jwtUtil;

    @Autowired
    UserEventLogMapper userEventLogMapper;

    @Autowired
    UserMapper userMapper;

    public String getUserToken(User user){
        Map<String, Object> userPayload = new HashMap<String, Object>();
        userPayload.put("id", user.getId());
        userPayload.put("username", user.getUserName());
        String token = JWTUtil.createJwtTokenpc(userPayload);
        return token;
    }

    public Result userInfoList(String token){

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token",token);
        User user = userMapper.selectOne(queryWrapper);

        if(user!=null) {
            QueryWrapper<UserEventLog> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("user_id", user.getId());
            List<UserEventLog> list = userEventLogMapper.selectList(queryWrapper2);
            if (list != null) {
                return Result.succ("成功",list);
            } else {
                return Result.fail("用户没有日志");
            }
        }else {
            return Result.fail("没有该用户");
        }
    }

    //微信端添加日志
    public void wxAddLog(User user,String msg){
        UserEventLog userLog = new UserEventLog();
        userLog.setCreateTime(new Date());
        userLog.setUserName(user.getUserName());
        userLog.setRealName(user.getRealName());
        userLog.setUserId(user.getId());
        userLog.setContent(msg);
        userEventLogMapper.insert(userLog);
    }
}

